using TMPro;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace UniRxExample
{
    /// <summary>
    /// UniRx在UI上应用案例
    /// </summary>
    public class UniRx_UIExample : MonoBehaviour
    {
        public Image urImage;
        
        public TextMeshPro urTextMP;
        
        public Toggle urToggle;
        
        public Slider urSlider;
        
        public Scrollbar urScrollbar;
        
        public Button urButton;
        
        public Dropdown urDropdown;
        
        public UnityEvent urUnityEvent;

        public void Start()
        {
            // UniRx_Scrollbar
            // 下滑进度条事件回调
            urScrollbar
                .ObserveEveryValueChanged(_ => urScrollbar.value)
                .Where(_ => _< 0.5f)
                .Subscribe(_ => Debug.Log("uniRx--->下拉一半")).AddTo(this);
            
            // UniRx_Image
            urImage.OnBeginDragAsObservable().Subscribe(_=> Debug.Log("uniRx--->OnBeginDrag"));
            urImage.OnDragAsObservable().Subscribe(_=> Debug.Log("uniRx--->OnDrag"));
            urImage.OnEndDragAsObservable().Subscribe(_=> Debug.Log("uniRx--->OnEndDrag"));
        }

        public void UniRx_Func()
        {
            var observable = Observable.Create<int>(observer =>
            {
                // 推送数据给订阅者
                observer.OnNext(1);
                observer.OnNext(2);
                observer.OnNext(3);
            
                // 完成数据流
                observer.OnCompleted();
            
                // 返回一个清理资源的 Disposable
                return Disposable.Empty;
            });
            
            // 订阅 Observable
            observable.Subscribe(
                onNext: value => Debug.Log($"Received: {value}"),
                onError: ex => Debug.Log($"Error: {ex.Message}"),
                onCompleted: () => Debug.Log("Completed!")
            );
        }
    }
}



















